package _08_blocking_queue.my_implements;

import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author: mornd
 * @dateTime: 2023/6/9 - 16:41
 */
public class MyTest {

    @Test
    public void test() throws Exception {
        var q = new _08_blocking_queue.my_implements.ArrayBlockingQueue<Integer>(2);
        q.offer(1);
        q.offer(2);

        Thread t1 = new Thread(() -> {
            try {
                q.put(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t1");
        t1.start();

        Thread t2 = new Thread(() -> {
            try {
                q.put(4);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t2");
        t2.start();

        Thread t3 = new Thread(() -> {
            try {
                System.out.println(q.take());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t3");
        t3.start();


        t1.join();
        t2.join();
        t3.join();
    }

    @Test
    public void test2()throws Exception {
        var q = new _08_blocking_queue.my_implements.ArrayBlockingQueue<Integer>(1);
        q.offer(1);
        q.take();

        Thread t1 = new Thread(() -> {
            try {
                System.out.println(q.take());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t1");
        t1.start();

        Thread t2 = new Thread(() -> {
            try {
                q.put(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t2");
        t2.start();


        t1.join();
        t2.join();
    }


    @Test
    public void test3() throws Exception {
        var q = new _08_blocking_queue.my_implements.ArrayBlockingQueue<Integer>(1);
        q.offer(1);
        System.out.println("start");
//        new Thread(() -> {
//            q.poll();
//        }).start();
        boolean offer = q.offer(2, 2, TimeUnit.SECONDS);
        System.out.println(offer);

    }

    public static void main(String[] args) {
        var q = new _08_blocking_queue.my_implements.ArrayBlockingQueue<Integer>(1);
//        q.offer(1);
        new Thread(() -> {
            q.offer(1);
        }).start();
        new Thread(() -> {
            try {
                q.take();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }

    @Test
    public void testJDKQueue() throws InterruptedException {
//        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(2);
        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(2);
        q.offer(1);
        q.offer(2);
        q.take();
        q.offer(3);
        System.out.println(q);
    }
}
